Skip to content

Restores switch state, case the switch is optimistic#14151

Merged
balloob merged 6 commits intohome-assistant:devfrom
dgomes:mqtt_restore_state
May 2, 2018
Merged

Restores switch state, case the switch is optimistic#14151
balloob merged 6 commits intohome-assistant:devfrom
dgomes:mqtt_restore_state

Conversation

@dgomes
Copy link
Copy Markdown
Contributor

@dgomes dgomes commented Apr 28, 2018

Description:

When the switch works in optimistic mode, HA is unaware of the current switch state and always defaults to False on restart.

This pull request restores the state of the switch to the last state, which is somehow what is expected from an optimistic switch (that he has kept the state since the last restart and not defaulted to False)

Example entry for configuration.yaml (if applicable):

switch:
  - platform: mqtt
    name: teste_switch
    command_topic: test/dummy_switch
    optimistic: true

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

@dgomes
Copy link
Copy Markdown
Contributor Author

dgomes commented Apr 28, 2018

If this PR gets through I will gladly update the mqtt platforms in the light component.

@dgomes dgomes changed the title Restores states in case the switch is optimistic Restores switch state, case the switch is optimistic Apr 28, 2018
Comment thread homeassistant/components/switch/mqtt.py Outdated
self.entity_id)
if last_state:
self._state = last_state.state
self.async_schedule_update_ha_state()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to schedule an update. We write the entity to the state machine right after this method is done.

Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge when a test is added.

Comment thread homeassistant/components/switch/mqtt.py Outdated

if self._optimistic:
last_state = await async_get_last_state(self.hass,
self.entity_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line over-indented for visual indent

Comment thread homeassistant/components/switch/mqtt.py Outdated

@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (85 > 79 characters)

@balloob balloob merged commit c851dfa into home-assistant:dev May 2, 2018
@balloob balloob mentioned this pull request May 11, 2018
@dgomes dgomes mentioned this pull request May 11, 2018
8 tasks
@dgomes dgomes mentioned this pull request May 28, 2018
3 tasks
@dgomes dgomes deleted the mqtt_restore_state branch August 17, 2018 22:09
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants